package com.itany.nmms.controller;

import com.github.pagehelper.PageInfo;
import com.itany.nmms.entity.Dept;
import com.itany.nmms.entity.Staff;
import com.itany.nmms.exception.CodeErrorException;
import com.itany.nmms.exception.RequestParameterErrorException;
import com.itany.nmms.exception.ServiceException;
import com.itany.nmms.exception.StaffNotExistException;
import com.itany.nmms.service.DeptService;
import com.itany.nmms.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 石小俊
 * @date 2024年04月17日 18:50
 */
@Controller
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    private StaffService staffService;
    @Autowired
    private DeptService deptService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(String pageNo, String pageSize, @ModelAttribute("staffParam") Staff staffParam) {
        ModelAndView mav = new ModelAndView();

        try {
            List<Dept> depts = deptService.findAll();
            mav.addObject("depts", depts);

            PageInfo<Staff> staffPage = staffService.findPage(pageNo, pageSize, staffParam);
            mav.addObject("staffPage", staffPage);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        mav.setViewName("backend/staffManager");
        return mav;
    }

    @RequestMapping("/login")
    public ModelAndView login(String loginName, String password, String role, String code, HttpSession session) {
        ModelAndView mav = new ModelAndView();
        String image = (String) session.getAttribute("code");

        try {
            Staff staff = staffService.login(loginName, password, role, code, image);
            // 如果登录成功,将当前员工信息保存到session中
            session.setAttribute("staff", staff);
            mav.setViewName("redirect:/showMain");
        } catch (RequestParameterErrorException e) {
            mav.addObject("loginMsg", e.getMessage());
            mav.setViewName("forward:/showLogin");
        } catch (CodeErrorException e) {
            mav.addObject("loginMsg", e.getMessage());
            mav.setViewName("backend/login");
        } catch (StaffNotExistException e) {
            mav.addObject("loginMsg", e.getMessage());
            mav.setViewName("backend/login");
        } catch (ServiceException e) {
            mav.addObject("loginMsg", e.getMessage());
            mav.setViewName("backend/login");
        }
        return mav;
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().invalidate();
        return "backend/login";
    }
}
